package com.dangdang.controller;

import com.dangdang.entity.Book;
import com.dangdang.entity.Category;
import com.dangdang.service.BookService;
import com.dangdang.service.CategoryService;
import com.dangdang.service.impl.BookServiceImpl;
import com.dangdang.service.impl.CategoryServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    //创建Book类业务对象
    private BookService bs = new BookServiceImpl();
    //创建Category业务类对象
    private CategoryService cs = new CategoryServiceImpl();
    //设置每页展示条数
    private final int pageSize = 5;
    @RequestMapping("/selectByChoice")
    public String selectByChoice(Model model){
        //编辑推荐  按销量正叙  2本
        List<Book> bookEdit = bs.selectByChoice("salenum asc", 2);
        //热门  按销量倒序  8本
        List<Book> bookHot = bs.selectByChoice("salenum desc", 8);
        //新书  按添加日期倒叙  8本
        List<Book> bookNew = bs.selectByChoice("groundingDate desc", 8);
        //新书热门 按销量倒序 8本
        List<Book> bookNewHot = bs.selectByChoice("groundingDate desc,salenum desc", 8);
        //分类浏览
        List<Category> cateList = cs.selectAllCate();
        model.addAttribute("bookEdit",bookEdit);
        model.addAttribute("bookHot",bookHot);
        model.addAttribute("bookNew",bookNew);
        model.addAttribute("bookNewHot",bookNewHot);
        model.addAttribute("cateList",cateList);
        return "forward:/main/main.jsp";
    }
    @RequestMapping("/showOne")
    public String showOne(int id, Model model){
        //根据id查询一本书  3表连接用于查询书的位置
        Book book = bs.showOne(id);
        model.addAttribute("book",book);
        return "forward:/main/product.jsp";
    }
    @RequestMapping("/selectByFid")
    public String selectByFid(int fid,@RequestParam(defaultValue = "1",value = "pageNo") int pageNo,Model model){
        //分页查询该一级类别下所有的书
        PageInfo<Book> pageInfo = bs.selectByFid(fid, pageNo, pageSize);
        //通过1级类别查询每个二级类别下有多少本书
        List<Category> categories = cs.selectCounts(fid);
        //通过id查询类别信息
        Category pcategory = cs.selectByCid(fid);
        model.addAttribute("categories",categories);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("fid",fid);
        model.addAttribute("pcategory",pcategory);
        return "forward:/main/book_list.jsp";
    }
    @RequestMapping("/selectBySid")
    public String selectBySid(int sid,@RequestParam(defaultValue = "1",value = "pageNo") int pageNo,Model model){
        //分页查询该二级类别下所有的书
        PageInfo<Book> pageInfo = bs.selectBySid(sid, pageNo, pageSize);
        //通过id查询类别信息 得到二级类别父类别id
        Category category = cs.selectByCid(sid);
        //通过二级类别父类别id 得到对应一级类别对象
        Category pcategory = cs.selectByCid(category.getPid());
        //分页查询该一级类别下所有的书 此时传入的一级类别id为通过二级类别查出来的
        List<Category> categories = cs.selectCounts(category.getPid());
        model.addAttribute("categories",categories);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("sid",sid);
        model.addAttribute("category",category);
        model.addAttribute("pcategory",pcategory);
        return "forward:/main/book_list.jsp";
    }
}
